package com.peng.leetcode.array;

/**
 * NumArray
 * <p>
 * 303. 区域和检索 - 数组不可变
 *
 * @author: lupeng6
 * @create: 2021/3/1 10:21
 */
public class NumArray1 {

    public static void main(String[] args) {
        int[] nums = {-2, 0, 3, -5, 2, -1};
        NumArray numArray = new NumArray(nums);
        System.out.println(numArray.sumRange(0, 2));
    }

    static class NumArray {

        private int[] sums;

        public NumArray(int[] nums) {
            this.sums = new int[nums.length + 1];
            for (int i = 0; i < nums.length; i++) {
                sums[i + 1] = nums[i] + sums[i];
            }
        }

        public int sumRange(int i, int j) {
            return sums[j + 1] - sums[i];
        }
    }

}
